const { default: BigNumber } = require('bignumber.js');
const { think } = require('thinkjs');
const Base = require('./base.js');

module.exports = class extends Base {
  async indexAction() {
    if (!this.isMobile) {
      return this.display('index/mobile_index.html');
    }

    if (think.isEmpty(this.get('job')) || think.isEmpty(this.get('company'))) {
      return this.display('index/404.html');
    }
    const data = await this.model('company_job')
      .where({
        'j.job_id': this.get('job'),
        'j.company_id': this.get('company'),
      })
      .alias('j')
      .join({
        post_list: {
          join: 'left',
          as: 'a',
          on: ['job_id', 'id'],
        },
        company_list: {
          join: 'left',
          as: 'c',
          on: ['company_id', 'id'],
        },
        // industry_list: {
        //   join: 'left',
        //   as: 'i',
        //   on: ['c.industry', 'id'],
        // },
      })
      .field(
        'a.name, a.desc, j.money_start, j.money_end, c.fuli, c.name as company_name, c.city, c.address, c.fuli'
      )
      .find();

    if (think.isEmpty(data)) {
      return this.display('index/404.html');
    }
    if (data.fuli) {
      data.tags = await this.model('tag_list')
        .where({
          id: ['IN', data.fuli],
        })
        .field('name, icon')
        .select();
    }

    // if (data.company) {
    //   data.company_num = data.company.split(',').length;
    // } else {
    //   data.company_num = false;
    // }

    // const companyInfo = await this.model('company_job')
    //   .where({ job_id: data.id })
    //   .select();

    // const companyInfoDistinct = await this.model('company_job')
    //   .where({ job_id: data.id })
    //   .distinct('company_id')
    //   .alias('a')
    //   .join({
    //     company_list: {
    //       join: 'left',
    //       as: 'p',
    //       on: ['company_id', 'id'],
    //     },
    //   })
    //   .field('p.avatar, p.name, p.city, p.scale, p.id, a.company_id')
    //   .select();

    // if (!think.isEmpty(companyInfo)) {
    //   companyInfo.sort((a, b) => a.money_end - b.money_end);
    //   data.max = companyInfo[companyInfo.length - 1].money_end;
    // } else {
    // }

    // if (!think.isEmpty(companyInfoDistinct)) {
    //   data.companys = companyInfoDistinct;
    // }

    // data.desc = encodeURI(data.desc);

    // const SchemeSerivce = this.service('weixin', 'index');
    // const url = await SchemeSerivce.getWxScheme(
    //   `id=${this.get('id')}&&company=${this.get('company')}&&phone=${this.get(
    //     'phone'
    //   )}`
    // );
    // if (url.openlink) {
    //   this.assign('openlink', url.openlink);
    // }

    this.assign('info', data);

    this.assign('id', this.get('job'));
    this.assign('company', this.get('company'));
    return this.display();
  }

  async submitAction() {
    if (think.isEmpty(this.get('id'))) {
      return this.display('index/404.html');
    }

    if (think.isEmpty(this.get('company'))) {
      return this.display('index/404.html');
    }

    if (think.isEmpty(this.get('phone'))) {
      return this.fail(408, '请填写手机号');
    }

    const data = await this.model('post_list')
      .where({ id: this.get('id') })
      .find();

    if (think.isEmpty(data)) {
      return this.display('index/404.html');
    }

    const SchemeSerivce = this.service('weixin', 'index');
    const url = await SchemeSerivce.getWxScheme(
      `id=${this.get('id')}&&phone=${this.get('phone')}&&company=${this.get(
        'company'
      )}`
    );
    return this.success({
      id: this.get('id'),
      openlink: url.openlink,
    });
  }
};
